﻿using DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.Models;
using DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.SiemensS7.Models;
using DeviceFaultPushService.MessagePushService.Models;
using S7.Net;
using DeviceInformationModel = DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.SiemensS7.Models.DeviceInformationModel;

namespace DeviceFaultPushService.DeviceProcessManagement.CommunicationManagement.SiemensS7;

public class ConfigurationGeneration
{

    private DeviceInformationModel _deviceInformation = new();

    public DeviceInformationModel DeviceInformation
    {
        get
        {
            return _deviceInformation;
        }
    }

    public void GenerationDeviceInformation(string number, string name, string protocol)
    {
        _deviceInformation.Number = number;
        _deviceInformation.Name = name;
        _deviceInformation.Protocol = (ProtocolEnum)Enum.Parse(typeof(ProtocolEnum), protocol, true);
    }

    public void GenerationDeviceAddress(string type, string ip, string port, string rack, string slot, string isMultiple)
    {
        _deviceInformation.DeviceAddress = new()
        {
            CpuType = (CpuType)Enum.Parse(typeof(CpuType), type.ToUpper(), true),
            Ip = ip,
            Port = short.Parse(port),
            Rack = short.Parse(rack),
            Slot = short.Parse(slot),
            IsMultiple = bool.Parse(isMultiple)

        };
    }

    public void GenerationContacts(string phoneNumber, string level, string enableDialing, string enableSms)
    {
        if (_deviceInformation.Contacts == null)
        {
            _deviceInformation.Contacts = new List<ContactsModel>();
        }
        _deviceInformation.Contacts.Add(new ContactsModel()
        {
            PhoneNumber = phoneNumber,
            UserLevel = (UserLevelEnum)Enum.Parse(typeof(UserLevelEnum), level, true),
            EnableSms = bool.Parse(enableSms),
            EnableDialing = bool.Parse(enableDialing)
        });
    }

    public void GenerationDataAddress(string alarmAddress, string isEnable, string alarmInformation)
    {
        if (alarmAddress.Length < 2)
        {
            return;
        }
        if (_deviceInformation.DataAddress == null)
        {
            _deviceInformation.DataAddress = new List<DataInformationModel>();
        }
        _deviceInformation.DataAddress.Add(new DataInformationModel()
        {
            AlarmAddress = alarmAddress.ToUpper(),
            IsEnable = bool.Parse(isEnable),
            AlarmInformation = alarmInformation
        });
    }

}

